package com.qf.contorller;

import com.qf.pojo.Cart;
import com.qf.pojo.User;
import com.qf.service.CartService;
import com.qf.service.CartServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author LiXuekai on 2022/5/25
 */
@WebServlet("/cart")
public class CartController extends BaseServlet {
    /**
     * 创建购物车
     * @param request
     * @param response
     * @return
     */
    public String create(HttpServletRequest request, HttpServletResponse response) {
        //1.获取商品的id
        String pid = request.getParameter("pid");
        //2.获取session会话中当前用户的id
        User user =(User)request.getSession().getAttribute("loginUser");
        if(user==null){
            //如果user等于null 说明用户没有登录  跳转到登录页面
            return  "redirect:login.jps";
        }
        //3.调用service业务层  将购物车添加到数据库中
        CartService cartService =new CartServiceImpl();
        cartService.createCart(user.getId(),Integer.parseInt(pid));
        //4跳转到购物车列表方法
            return "redirect:/cart?method=show";
    }

    /**
     * 购物车管理
     * 查询指定用户的购物车数据  并跳转到购物车列表页面展示
     *
     * @param request
     * @param response
     * @return
     */
    public String show(HttpServletRequest request, HttpServletResponse response) {
        //1 获取session会话中当前用户的id
        User user =(User) request.getSession().getAttribute("loginUser");
        if(user==null){
            //用户未登录
            return "redirect:/login.jsp";
        }
        //2获取用户的id调用service查询这个用户的购物车数据
        CartService cartService=new CartServiceImpl();
       List<Cart> cartList= cartService.findCartListByUid(user.getId());
        //3 将用户购物车的数据放入request域中 并带到页面展示
            request.setAttribute("list", cartList);
        //4. 跳转到购物车列表页面展示
        return "forward:/cart.jsp";
    }

    /**
     * 修改购物车购买数量的功能
     * @param request
     * @param response
     * @return
     */
    public String update(HttpServletRequest request, HttpServletResponse response) {
        //1.从会话中获取当前登录用户的id 如果获取不到就跳转到登录界面
        User user =(User) request.getSession().getAttribute("loginUser");
        if(user==null){
            return "redirect:/login.jsp";

        }
        //2.获取购物主键的id参数
        String cid = request.getParameter("cid");
        //获取购买的数量
        String cnum = request.getParameter("cnum");
        //3.调用service，执行修改业务
        CartService cartService=new CartServiceImpl();
        cartService.updateCart(Integer.parseInt(cid),Integer.parseInt(cnum));
        //4.跳转到购物车列表方法 查看购物车中最新的数据展示
        return "redirect:/cart?method=show";
    }

    /**
     * 根据商品主键id删除商品
     *
     * @param request
     * @param response
     * @return
     */
    public String delete(HttpServletRequest request,HttpServletResponse response){
        //1.到session会话中获取当前的登录对象  获取不到 说明没有登录 跳转到登录揭秘那
        User user =(User) request.getSession().getAttribute("loginUser");
        if(user==null){
            //用户未登录重新登录  跳转到登录界面
            return "redirect:/login.jsp";
        }
        //2.获取商品主键的id
        String cid = request.getParameter("cid");
        //3.调用service根据购物的主键id删除
        CartService cartService=new CartServiceImpl();
        cartService.deleteById(Integer.parseInt(cid));
        //4.跳转到购物车列表查询页面 显示最新的购物数据
        return "redirect:/cart?method=show";
    }
    }

